#include "IRQ.h"


// 初始化中断控制器ScuGic
int scuGic_Inti(XScuGic *scuGicPtr, u16 scuGicID)
{
	XScuGic_Config *scuGicConfig;
	scuGicConfig = XScuGic_LookupConfig(scuGicID); // 根据器件ID查找配置

	int status;
	status = XScuGic_CfgInitialize(&scuGic, scuGicConfig, scuGicConfig->CpuBaseAddress);
	if (status != XST_SUCCESS)
	{
		xil_printf("ScuGic Initialization Failed\r\n");
		return XST_FAILURE;
	}

	// 打开系统的中断处理功能, 这一段需要在XScuGic_CfgInitialize函数段之后，否则程序会卡死在Xil_ExceptionEnable

	Xil_ExceptionInit(); // 初始化异常句柄，只在很早版本的bsp中需要，此处为了兼容性保留

	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler,
			                     scuGicPtr); // 为IRQ注册中断处理程序

	Xil_ExceptionEnable(); // 使能系统中断功能

	status = XScuGic_SelfTest(scuGicPtr);
	if (status != XST_SUCCESS)
	{
		xil_printf("ScuGic SelfTest Failed\r\n");
		return XST_FAILURE;
	}

	xil_printf("ScuGic Initialization Succeed\r\n");
	return status;
}


// 设置中断优先级与类型，关联中断处理函数并启用对应ID的中断源
void scuGic_Set_ConnectAndEnable(XScuGic *scuGicPtr, u32 Int_Id, u8 Priority, u8 Trigger,
		                         Xil_ExceptionHandler X_Handler,  void *CallBackRef)
{
	// 设置中断优先级和类型
	XScuGic_SetPriorityTriggerType(scuGicPtr, Int_Id, Priority, Trigger);

	// 连接中断服务函数
	XScuGic_Connect(scuGicPtr, Int_Id, (Xil_ExceptionHandler)X_Handler, CallBackRef);

	// 启用对应中断ID的中断源
	XScuGic_Enable(scuGicPtr, Int_Id);
}
